package net.mingsoft.dashboard.action.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.dashboard.action.q.collection.*;
import net.mingsoft.dashboard.action.r.CollectionDirResponse;
import net.mingsoft.dashboard.action.r.CreateOrModifyCollectionResponse;
import net.mingsoft.dashboard.action.r.FileCollectionResponse;
import net.mingsoft.dashboard.biz.MyCollectionBiz;
import net.mingsoft.dashboard.entity.collection.FileCollectionEntity;
import net.mingsoft.document.biz.FileStarBiz;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author gengfa
 * @date 22/01/25
 */
@Api(value = "我的收藏", tags = {"工作台-我的收藏"})
@Controller("WebCollectionAction")
@RequestMapping("/${ms.manager.path}/collection")
public class MyCollectionAction {

    @Resource
    MyCollectionBiz collectionBiz;

    @Resource
    FileStarBiz fileStarBiz;

    @ApiOperation(value = "创建收藏夹")
    @PostMapping(value = "/create", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "success", response = CreateOrModifyCollectionResponse.class)
    })
    @ResponseBody
    public ResultData create(@RequestBody CollectionCreateQuery query) {
        if (query.validate()) {
            CreateOrModifyCollectionResponse response = collectionBiz.createCollection(query);
            return ResultData.build().success(response);
        }
        return ResultData.build().error();
    }

    @ApiOperation(value = "收藏列表")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "success", response = FileCollectionEntity.class),
            @ApiResponse(code = 200, message = "success", response = FileCollectionResponse.class)
    })
    @PostMapping(value = "/list", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResultData list(@RequestBody CollectionListQuery query) {
        Page page = collectionBiz.list(query);
        return ResultData.build().success(page);
    }

    @ApiOperation(value = "收藏夹目录")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "success", response = CollectionDirResponse.class)
    })
    @GetMapping(value = "/dir")
    @ResponseBody
    public ResultData dir() {
        List<CollectionDirResponse> dirs = collectionBiz.dir();
        return ResultData.build().success(dirs);
    }

    @ApiOperation(value = "删除/批量删除")
    @PostMapping(value = "/delete", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResultData delete(@RequestBody CollectionDeleteQuery query) {
        if (collectionBiz.deleted(query)) {
            return ResultData.build().success();
        }
        return ResultData.build().error();
    }

    @ApiOperation(value = "修改名称")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "success", response = CreateOrModifyCollectionResponse.class)
    })
    @PostMapping(value = "/modify", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResultData modify(@RequestBody CollectionModifyQuery query) {
        if (query.validate()) {
            CreateOrModifyCollectionResponse response = collectionBiz.modifyName(query);
            return ResultData.build().success(response);
        }
        return ResultData.build().error();
    }

    @ApiOperation(value = "取消收藏")
    @PostMapping(value = "/cancel")
    @ResponseBody
    public ResultData cancel(@RequestBody CollectionCancelQuery query) {
        if (fileStarBiz.cancelStar(query)) {
            return ResultData.build().success();
        }
        return ResultData.build().error();
    }
}
